package com.orange.notificationsummary.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings;
import android.provider.Settings.System;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;

import com.orange.homescreen.pil.DateTimeInterface;
import com.orange.homescreen.pil.PILInterfaceManager;
import com.orange.notificationsummary.R;
import com.orange.notificationsummary.entry.ConversationColumns;
import com.orange.notificationsummary.utils.Constants.Preference_Key;

public class EventLSWidgetUtils implements ConversationColumns {
	private static String		TAG			= "EventLSWidgetUtils";

	public static final int[]	DAYOFWEEK	= { R.string.dayOfWeek_Sunday,
			R.string.dayOfWeek_Monday, R.string.dayOfWeek_Tuesday,
			R.string.dayOfWeek_Wednesday, R.string.dayOfWeek_Thursday,
			R.string.dayOfWeek_Friday, R.string.dayOfWeek_Saturday };

	public static final int[]	MONTHOFYEAR	= { R.string.monthOfYear_January,
			R.string.monthOfYear_February, R.string.monthOfYear_March,
			R.string.monthOfYear_April, R.string.monthOfYear_May,
			R.string.monthOfYear_June, R.string.monthOfYear_July,
			R.string.monthOfYear_August, R.string.monthOfYear_September,
			R.string.monthOfYear_October, R.string.monthOfYear_November,
			R.string.monthOfYear_December	};

	public static final String	FLAG		= "+";
	public static final String	SPACE		= " ";
	public static final String	COLON		= ":";

	public static String numberFormat(String number) {
		String numberRslt = "";
		if (!TextUtils.isEmpty(numberRslt)) {
			numberRslt = PhoneNumberUtils.formatNumber(number);
		}
		return numberRslt;
	}

	public static List<String> listFormat(List<String> list) {
		Object[] strArray = list.toArray();
		int count = strArray.length;
		for (int i = 0; i < count; i++) {
			String tempJ = (String) strArray[i];
			for (int j = i + 1; j < count; j++) {
				String tempK = (String) strArray[j];
				if (tempJ.equals(tempK)) {
					strArray[j] = FLAG;
				}
			}
		}
		List<String> listRslt = new ArrayList<String>();
		for (int i = 0; i < strArray.length; i++) {
			String temp = (String) strArray[i];
			if (!FLAG.equals(temp)) {
				listRslt.add(temp);
			}
		}
		return listRslt;
	}

	public static String bodyFormat(String body, int length) {
		String bodyRslt;
		if (body == null || (body.trim()).equals("")) {
			bodyRslt = "";
		} else {
			int bodyLength = body.length();
			if (bodyLength > length) {
				bodyRslt = body.substring(0, length);
			} else {
				bodyRslt = body;
			}
		}
		return bodyRslt;
	}

	public static boolean getEventPreviewSetting(Context context) {
		Context otherAppContext = null;
		boolean isPrivacy = false;

		try {
			otherAppContext = context.createPackageContext(
					Constants.PKG_LOCKSCREEN_SETTING,
					Context.CONTEXT_IGNORE_SECURITY);
			if (null != otherAppContext) {
				SharedPreferences sp = otherAppContext.getSharedPreferences(
						Constants.PREFERENCES_SETTINGS_NAME,
						Context.MODE_WORLD_READABLE);
				isPrivacy = sp.getBoolean("preference_widget_event_detail",
						false);
			}
		} catch (NameNotFoundException e) {
		}

		return isPrivacy;
	}

	public static boolean getNotifictionTypesIsChecked(Context context,
			String key) {

		Context otherAppContext = null;
		try {
			otherAppContext = context.createPackageContext(
					Constants.PKG_NOTIFICATION_SUMMARY,
					Context.CONTEXT_IGNORE_SECURITY);

		} catch (NameNotFoundException e) {
			LogHelper.e(TAG, "getNotifictionTypesIsChecked. ", e);
		}

		boolean isChecked = false;
		//modify by gaoge 2011-12-23,because here would cause NullPointerException
		if(null != otherAppContext ){
			SharedPreferences sp = otherAppContext.getSharedPreferences(
					"com.orange.lockscreen.preferences",
					Context.MODE_WORLD_READABLE);
			isChecked = sp.getBoolean(key, true);
			LogHelper.e(TAG, "getNotifictionTypesIsChecked. key: " + key
					+ ", ischecked: " + isChecked);
		}
		
		return isChecked;
	}

	public static String dateFormat(Context context, long str) {
		String formatted = null;
		DateTimeInterface interfc = (DateTimeInterface) PILInterfaceManager
				.getInterface(DateTimeInterface.class);

		String time = interfc.getSystemFormattedTime(context, str);

		if (time.substring(0, 2).equalsIgnoreCase("24")) {
			time = "00" + time.substring(2, time.length());
		}

		if (interfc.isToday(str)) {
			String today = context.getString(R.string.today);

			if (today == null) {
				today = "Today";
			}
			formatted = String.format("%s %s", today, time);
		} else if (interfc.isInThisYear(str)) {
			formatted = interfc.getFormattedDate(getDataFormat(context, false),
					str);
			formatted = formatted + ", " + time;
			formatted = getDayOfWeek(context, str) + " " + formatted;
		} else {
			formatted = interfc.getFormattedDate(getDataFormat(context, true),
					str);
			formatted = formatted + ", " + time;
			formatted = getDayOfWeek(context, str) + " " + formatted;
		}
		return (formatted == null ? "" : formatted);

	}

	public static String getDayOfWeek(Context context, long milliseconds) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(milliseconds);
		int mDayOfWeek = calendar.get((Calendar.DAY_OF_WEEK)) - 1;
		return context.getResources().getString(DAYOFWEEK[mDayOfWeek]);
	}

	// "MMM dd yyyy"
	public static String getDataFormat(Context context, Boolean year) {
		String dateFormat = Settings.System.getString(
				context.getContentResolver(), System.DATE_FORMAT);
		boolean b = true;
		// add by gaoge 2011-09-16,for fix bug 0013488,when select normal date
		// format,dateFormat length is 0
		// in this condition,the handset is zte_crescent or acer 6,which default
		// dateFormat is dd-MM-yyyy
		if (null != dateFormat && 0 == dateFormat.length()) {
			dateFormat = "dd-MM-yyyy";
		}
		// if dateFormat is null,indicate that the handset is nexous one,which
		// default dateFormat is yyyy-dd-MM
		if (null == dateFormat) {
			dateFormat = "yyyy-dd-MM";
		}
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < dateFormat.length(); i++) {
			char a = dateFormat.charAt(i);

			if (year && 'y' == a) {
				buffer.append(a);
			} else if ('-' == a) {
				buffer.append(' ');
			} else if ('M' == a) {
				if (b) {
					buffer.append("MM");
					b = false;
				} else {
					buffer.append(a);
				}
			} else if ('d' == a) {
				buffer.append(a);
			}
		}
		return buffer.toString().trim();
	}

	public static boolean isPackageExist(Context context, String packageName) {
		try {
			context.getPackageManager().getPackageInfo(packageName,
					PackageManager.GET_SERVICES);
		} catch (NameNotFoundException e) {
			return false;
		}

		return true;
	}

	public static boolean isNotificationScreenEnabled(Context context) {
		boolean enabled = true;
		try {
			Context otherContext = context.createPackageContext(
					Constants.PKG_LOCKSCREEN_SETTING,
					Context.CONTEXT_IGNORE_SECURITY);
			if (null != otherContext) {
				SharedPreferences sp = otherContext.getSharedPreferences(
						Constants.PREFERENCES_SETTINGS_NAME,
						Context.MODE_WORLD_WRITEABLE);

				enabled = sp.getBoolean(
						Preference_Key.KEY_VIEW_NOTIFICATION_ENABLE, true);
			}
		} catch (NameNotFoundException e) {
		}
		return enabled;
	}

}
